<?php
    require_once($GLOBALS['ROO'].'Libraries/Objects/val.form.mex');
MYDCONN();
	$ItemProperties = $GLOBALS['MYD']->db_array("SELECT Status, TYPE FROM ccenter_invoices WHERE ref = {$_POST['RefInvoice']}");
	
        if(isset($ItemProperties[1]) && $ItemProperties[1]['Status'] == 1 && in_array($ItemProperties[1]['TYPE'],array(8,9))==true || $_SESSION[$SESSOF] < 9) {

	$art_Code = 0;
	if(isset($_POST['Type'])) { $art_Code = $_POST['CODE']; }
	if(!isset($_POST['User'])) {$_POST['User']=0;}
	
        $_POST['ArtQuantity'] = frm_FormatDecimal($_POST['ArtQuantity']);
	if (isset($_POST['ArtAmount']) && isset($_POST['ArtTYP']) && $_POST['ArtTYP'] == 1) {		$_POST['ArtAmount'] = 0-$_POST['ArtAmount'];}
	$_POST['ArtUAmount'] = frm_FormatDecimal($_POST['ArtUAmount']);
        $_POST['ArtTaxPercent'] = frm_FormatDecimal($_POST['ArtTaxPercent']);
        if (isset($_GET['ACT']) && $_GET['ACT'] == 'RT') {
	$GLOBALS['MYD']->db_query("UPDATE ccenter_invarticles
			SET Status = 5
			WHERE Ref = {$_GET['RCINV']}");
	} elseif (isset($_GET['ACT']) && $_GET['ACT'] == 'ED') {
	$GLOBALS['MYD']->db_query("UPDATE ccenter_invarticles
			SET CODE = {$_GET['CODE']}
			WHERE Ref = {$_GET['RCINV']}");
	} else {
	$_POST['ArtReference'] = 0;
	if ($_POST['User'] == '') { $_POST['User'] = 0; }
	if(!isset($_POST['DateTime'])) { $_POST['DateTime'] = 'null'; } else {$_POST['DateTime'] = "'".$_POST['DateTime']."'";}
	$GLOBALS['MYD']->db_query("INSERT INTO ccenter_invarticles
			(RefInvoice, ArtReference, Description, Quantity, UAmount, IVAPercent, Amount, Promo, Status, User, Office, TmpInvoice, TYPE, CODE, sysTime, sysUser)
			VALUES
			({$_POST['RefInvoice']},{$_POST['ArtReference']},'{$_POST['ArtDescription']}',{$_POST['ArtQuantity']},'{$_POST['ArtUAmount']}','{$_POST['ArtTaxPercent']}',
                        '{$_POST['ArtAmount']}','{$_POST['ArtPromo']}', 1, {$_POST['User']}, {$_POST['Office']},{$_POST['RefInvoice']},{$_POST['ArtTYP']},$art_Code,{$_POST['DateTime']},$_SESSION[$SESSID])");
	}
	} else {
		echo 'Erro:Nao é possivel alterar';
	}
	if (!isset($_POST['Type'])) {
	include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/list_invoicearticles.php');
	} else {
	include($GLOBALS['PAD'].'Modules/CentrodeCustos/Lib/list_invotherarticles.php');
	}